package com.sgb.kjwl.viewmodel.self;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.lifecycle.MutableLiveData;

import com.harisucici.jetpackbase.base.HBaseViewModel;
import com.sgb.kjwl.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.sgb.kjwl.model.entity.user.CompanyInfoEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.xrecyclerview.XRecyclerView;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JoinCompanyModel extends HBaseViewModel {
    public ObservableField<List<CompanyInfoEntity>> companyInfoEntityList = new ObservableField();
    public MutableLiveData<Boolean> isRefreshEnd;
    private int page = 1;
    private int size = 10;
    public String text = "";


    public JoinCompanyModel(@NonNull @NotNull Application application) {
        super(application);
    }


    public MutableLiveData<Boolean> getIsRefreshEnd() {
        if (isRefreshEnd == null) {
            isRefreshEnd = new MutableLiveData<>();
        }
        return isRefreshEnd;
    }

    public XRecyclerView.LoadingListener loadingListener = new XRecyclerView.LoadingListener() {
        @Override
        public void onRefresh() {
            getCompanyListModel(text, false);
        }

        @Override
        public void onLoadMore() {
            getCompanyListModel(text, true);
        }
    };

    @Override
    public void onResume() {
        super.onResume();
        getCompanyListModel(text, false);
    }

    public void getCompanyListModel(String text, boolean isLoadMore) {
        this.text = text;
        page = isLoadMore ? page + 1 : 1;
        isRefreshEnd.postValue(false);
        Map<String, Object> map = new HashMap();
        map.put("pageNum", page);
        map.put("pageSize", size);
        map.put("text", text);
        APIManager.getInstance().getUserAPI().searchCompanyByText(map)
                .enqueue(new ICallBack<BaseEntity<ListResponse<List<CompanyInfoEntity>>>>() {
                    @Override
                    public void onSuccess(BaseEntity<ListResponse<List<CompanyInfoEntity>>> data) {
                        isRefreshEnd.postValue(true);
                        companyInfoEntityList.set(data.getData().getList());
                    }
                });
    }

}
